package com.earnings.okhttputils.utils.car.ui;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.earnings.R;
import com.earnings.okhttputils.utils.bean.CarColor;
import com.earnings.okhttputils.utils.bean.CarDetailsData;
import com.earnings.okhttputils.utils.bean.SelectData;
import com.earnings.okhttputils.utils.god.GodAdapter.GodArrayList;
import com.earnings.okhttputils.utils.god.GodAdapter.GodBaseAdapter;
import com.earnings.okhttputils.utils.god.GodAdapter.GodViewHolder;
import com.earnings.okhttputils.utils.view.Circular;

/**
 * Created by Administrator on 2017/6/19 0019.
 */

public class SelectDialog extends Dialog implements View.OnClickListener {
    private ImageView img_view;
    private TextView money_tv, waicolor_tv, neicolor_tv, confirm_tv;
    private RecyclerView recyclerView1, recyclerView2;

    public SelectDialog(@NonNull Context context) {
        super(context, R.style.selectsize_dialog);
        setContentView(R.layout.dialog_select);
        getWindow().setGravity(Gravity.BOTTOM); //显示在底部
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        //设置dialog的宽度为当前手机屏幕的宽度
        p.width = d.getWidth();
        getWindow().setAttributes(p);
        initView();
    }

    private void initView() {
        img_view = (ImageView) findViewById(R.id.img_view);
        money_tv = (TextView) findViewById(R.id.money_tv);
        waicolor_tv = (TextView) findViewById(R.id.waicolor_tv);
        neicolor_tv = (TextView) findViewById(R.id.neicolor_tv);
        confirm_tv = (TextView) findViewById(R.id.confirm_tv);
        recyclerView1 = (RecyclerView) findViewById(R.id.recyclerview1);
        recyclerView2 = (RecyclerView) findViewById(R.id.recyclerview2);
        recyclerView1.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        recyclerView2.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        confirm_tv.setOnClickListener(this);
    }

    public void setData(CarDetailsData data) {
        money_tv.setText("指导价：￥" + data.getGprice() + "万");
        Glide.with(getContext()).load(data.getThumb()).into(img_view);
        GodArrayList<CarColor> wailist = new GodArrayList();
        final GodArrayList<CarColor> neilist = new GodArrayList();
        wailist.addAll(data.getWaicolor());
        neilist.addAll(data.getNeicolor());
        GodBaseAdapter waiadapter = new GodBaseAdapter<CarColor>(R.layout.item_select, wailist) {
            @Override
            public void onBind(GodViewHolder holder, int position, CarColor data) {
                Integer colorbg = str2Color(data.getBg());
                holder.getView(R.id.bg_view).setBackgroundColor(colorbg);
                if (getOptindex() == position) holder.setVisibility(R.id.opt_view, View.VISIBLE);
                else holder.setVisibility(R.id.opt_view, View.GONE);
            }

            @Override
            public void OnItemClickListener(View view, int position, CarColor data) {
                waicolor_tv.setText("已选择：" + data.getName());
            }
        };
        waicolor_tv.setText("已选择：" + wailist.get(0).getName());
        waiadapter.setOneChoice(true);
        waiadapter.setDefaultposition(0);
        recyclerView1.setAdapter(waiadapter);

        GodBaseAdapter neiadapter = new GodBaseAdapter<CarColor>(R.layout.item_select, neilist) {
            @Override
            public void onBind(GodViewHolder holder, int position, CarColor data) {
                Integer colorbg = str2Color(data.getBg());
                holder.getView(R.id.bg_view).setBackgroundColor(colorbg);
                if (getOptindex() == position) holder.setVisibility(R.id.opt_view, View.VISIBLE);
                else holder.setVisibility(R.id.opt_view, View.GONE);
            }

            @Override
            public void OnItemClickListener(View view, int position, CarColor data) {
                neicolor_tv.setText("已选择：" + data.getName());
            }
        };
        neicolor_tv.setText("已选择：" + neilist.get(0).getName());
        neiadapter.setOneChoice(true);
        neiadapter.setDefaultposition(0);
        recyclerView2.setAdapter(neiadapter);
    }

    public static Integer str2Color(String colorString) {
        Integer color = null;
        if (colorString != null) {
            if (colorString.length() > 1) {
                int index = colorString.indexOf("#");
                if (index == 0) {
                    colorString = colorString.substring(1, colorString.length());
                    if (colorString.length() == 8) {
                        color = Integer.valueOf(colorString, 16);
                    } else if (colorString.length() == 6) {
                        color = 0xff000000 | Integer.valueOf(colorString, 16);
                    }
                }
            }
        }
        return color;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.confirm_tv) dismiss();

    }
}
